﻿Imports Data

Public Class IngresoPago
    Private Property id_pagoDeuda As Integer
    Private Property id_accion As Integer

    Public Enum Accion
        Ingresar = 1
        Modificar = 2
    End Enum

    Private Sub IngresoPago_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.CargaLista()
        If id_accion = Accion.Modificar Then
            Me.Accion_Modificar()
        End If
    End Sub

    Private Sub CargaLista()
        Me.CargarCBXEmpleado()
        Me.CargarCBXFormaPago()
        Me.CargarCBXModoPago()
        Me.CargarMes()
    End Sub

    Private Function Accion_Modificar() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim pago As Data.Pago_Empleados = db.Pago_Empleados.Where(Function(p) p.id = Me.id_pagoDeuda).Select(Function(p) p).FirstOrDefault
                Me.cbxEmpleado.SelectedValue = pago.id_empleado
                Me.cbxFormaPago.SelectedValue = pago.id_forma_pago
                Me.cbxModoPago.SelectedValue = pago.id_modo_pago
                Me.dtFechaPago.Value = pago.fecha_pago
                Me.txtExtra.Text = pago.extra
                Me.txtMonto.Text = pago.monto
                Me.txtObservacion.Text = pago.observaciones
                Me.CargarMes()
                Me.btnIngresarPagoSueldo.Text = "Modificar"
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

#Region "Carga Listas"

    Private Function CargarCBXEmpleado() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Empleados _
                                                        Select New AbstractObject With {.ID = x.id, _
                                                                                   .Nombre = String.Format("{0} {1} {2}", x.nombre, x.apellido_paterno, x.apellido_materno)}).ToList


                Dim manager As BindingManagerBase = Me.cbxEmpleado.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxEmpleado.DisplayMember = "Nombre"
                Me.cbxEmpleado.ValueMember = "ID"
                Me.cbxEmpleado.DataSource = lista
                Me.cbxEmpleado.Enabled = True
                manager.ResumeBinding()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function CargarCBXModoPago() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of AbstractObject) = (From x In db.Modo_Pago _
                                                      Select New AbstractObject With {.ID = x.id, _
                                                                                      .Nombre = x.nombre}).ToList

                Dim manager As BindingManagerBase = Me.cbxModoPago.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxModoPago.DisplayMember = "Nombre"
                Me.cbxModoPago.ValueMember = "ID"
                Me.cbxModoPago.DataSource = lista
                Me.cbxModoPago.Enabled = True
                manager.ResumeBinding()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function CargarCBXFormaPago() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext

                Dim lista As List(Of AbstractObject) = (From x In db.Forma_Pago _
                                               Select New AbstractObject With {.ID = x.id, _
                                                                               .Nombre = x.nombre}).ToList


                Dim manager As BindingManagerBase = Me.cbxFormaPago.BindingContext(lista)

                manager.SuspendBinding()
                Me.cbxFormaPago.DisplayMember = "Nombre"
                Me.cbxFormaPago.ValueMember = "ID"
                Me.cbxFormaPago.DataSource = lista
                Me.cbxFormaPago.Enabled = True
                manager.ResumeBinding()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function CargarMes() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim n_mes As Integer = Me.dtFechaPago.Value.Month
                Dim mes As String = db.Meses.Where(Function(p) p.id = n_mes).Select(Function(p) p.nombre).FirstOrDefault
                Me.lblMesDePago.Text = mes
            End Using
        Catch ex As Exception

        End Try
    End Function

#End Region


  

    Private Sub dtFechaPago_ValueChanged(sender As System.Object, e As System.EventArgs)
        Try
            Me.CargarMes()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnIngresarPagoSueldo_Click(sender As System.Object, e As System.EventArgs) Handles btnIngresarPagoSueldo.Click
        Try
            If Me.id_accion = Accion.Modificar Then
                If (Me.Modificar()) Then
                    MsgBox("Modificado con exito")
                    Me.Close()
                End If
            Else
                Dim n_pago As New Data.Pago_Empleados
                n_pago.id_empleado = Me.cbxEmpleado.SelectedValue
                n_pago.fecha_pago = Me.dtFechaPago.Value
                n_pago.id_mes = Me.dtFechaPago.Value.Month
                n_pago.extra = Me.txtExtra.Text
                n_pago.id_forma_pago = Me.cbxFormaPago.SelectedValue
                n_pago.id_modo_pago = Me.cbxModoPago.SelectedValue
                n_pago.monto = Me.txtMonto.Text
                n_pago.observaciones = Me.txtObservacion.Text
                n_pago.id_estado = 2
                If (Me.Ingresar(n_pago)) Then
                    MsgBox("Ingresado con exito")
                    Me.Close()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Function Ingresar(ByVal pago As Data.Pago_Empleados) As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                db.Pago_Empleados.InsertOnSubmit(pago)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function


    Private Function Modificar() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim m_pago As Data.Pago_Empleados = db.Pago_Empleados.Where(Function(p) p.id = Me.id_pagoDeuda).Select(Function(p) p).FirstOrDefault
                m_pago.id_empleado = cbxEmpleado.SelectedValue
                m_pago.fecha_pago = dtFechaPago.Value
                m_pago.extra = Me.txtExtra.Text
                m_pago.id_forma_pago = Me.cbxFormaPago.SelectedValue
                m_pago.id_modo_pago = Me.cbxModoPago.SelectedValue
                m_pago.monto = Me.txtMonto.Text
                m_pago.observaciones = Me.txtObservacion.Text
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function


    Public Sub New()
        InitializeComponent()
    End Sub

    Public Sub New(ByVal id_accion As Integer, ByVal id_pago As Integer)
        InitializeComponent()
        Me.id_accion = id_accion
        Me.id_pagoDeuda = id_pago
    End Sub
End Class